/**
 * Created by zlp on 2018/01/04.
 * For 我的账户
 */
var Promise = require("bluebird");
var request = require("superagent");
var API = require("../support/const");
var setting = require("../../common/cn/sale/index");
var memberSetting = require("../../common/cn/member/index");
exports.web = {
    home: function (req, res, next) {
        res.render("pages/web/index")
    },
    login: function (req, res, next) {
        res.render("pages/web/login")
    },
    realname: function (req, res, next) {
        res.render("pages/web/realname")
    },
    register: function (req, res, next) {
        res.render("pages/web/register")
    },
    registercomplete: function (req, res, next) {
        res.render("pages/web/registercomplete")
    },
    invest: function (req, res, next) {
        res.render("pages/web/invest")
    },
    support: function (req, res, next) {
        res.render("pages/web/support")
    },
    news: function (req, res, next) {
        res.render("pages/web/news")
    },
    article: function (req, res, next) {
        res.render("pages/web/article")
    },
    findLoginPassword: function (req, res, next) {
        res.render("pages/web/loginPassword")
    },
    findTradePassword: function (req, res, next) {
        res.render("pages/web/tradePassword")
    }

};

let memberResult = {
    datas: {},
    setting: memberSetting
};
exports.account = {

    index: function (req, res, next) {
        res.render("pages/account/home")
    },
    fundRecord: function (req, res, next) {
        res.render("pages/account/fundRecord")
    },
    transaction: function (req, res, next) {
        res.render("pages/account/transaction")
    },

    earning: function (req, res, next) {
        res.render("pages/account/earning")
    },
    bonus: function (req, res, next) {
        res.render("pages/account/bonus")
    },
    userinfo: function (req, res, next) {
        res.render("pages/account/userinfo", )
    },
    bankcard: function (req, res, next) {
        let header = {
            token: req.cookies.token
        };


        let data = {};

        memberResult.banksInfo = JSON.stringify(memberSetting.select.account.bank);
        function render() {
            //templateResult.active = 'userinfo';
            memberResult.datas = data;
            res.render("pages/account/bankcard",memberResult);
        }

        function getBasic() {
            request.get(API.web.personal_center.base_info)
                .set(header)
                .end(function (_req, _res) {
                    data.basic = JSON.parse(JSON.parse(_res.text).datas);
                    getBankcards();
                });
        }

        function getBankcards() {
            request.get(API.web.personal_center.bank_cards)
                .set(header)
                .end(function (_req, _res) {
                    data.bankcards = JSON.parse(JSON.parse(_res.text).datas);
                    render();
                });
        }

        getBasic();

    },
    partner: function (req, res, next) {
        res.render("pages/account/partner")
    },

};

let templateResult = {
    datas: {},
    setting: setting
};
exports.sale = {

    login: function (req, res, next) {
        res.render("pages/sale/pages/login/login")
    },
    retrieve: function (req, res, next) {
        res.render("pages/sale/pages/login/retrieve")
    },
    retrieve_reset: function (req, res, next) {
        
        res.render("pages/sale/pages/login/retrieve_reset")
    },
    //个人信息
    userinfo: function (req, res, next) {
        templateResult.active = "userinfo";

        let header = {
            token: req.cookies.token
        };

        request.get(API.sale.sale_info.info)
            .query({})
            .set(header)
            .end(function (_req, _res) {

                let data = JSON.parse(_res.text);
                if(data.status == 200){
                    let datas = JSON.parse(data.datas);
                    templateResult.datas = datas;
                }
                res.render("pages/sale/pages/userinfo/index",templateResult);
            });
    },
    custpage: function (req, res, next) {
        templateResult.active = "custpage";
        res.render("pages/sale/pages/custInfo/custList",templateResult)
    },
    deductpage: function (req, res, next) {
        templateResult.active = "deductpage";
        res.render("pages/sale/pages/deduct/sellDeduct",templateResult)
    }

};